﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI.WebControls;
using PortalCurriculo.Domain;
using PortalCurriculo.Util.CustomControls.ListViewObjects;
using PortalCurriculo.Util.Translation;
using System.Linq;

namespace PortalCurriculo.WebUI.Helpers
{
    public class PageHelper : WebPage
    {
        #region Propriedades

        protected Empresa EmpresaLogada
        {
            get { return CompanyAccessHelper.CompanyLogged; }
        }

        protected Usuario UsuarioLogado
        {
            get { return GeneralAccess.UserLogged; }
        }

        protected Profissional ProfissionalLogado
        {
            get { return ProfessionalAccessHelper.ProfessionalLogged; }
        }

        protected List<Empresa> EmpresasSelecionadorLogado
        {
            get { return RecruiterAccessHelper.RecruiterCompaniesLogged; }
        }

        protected Administrador AdministradorLogado
        {
            get { return AdministratorAccessHelper.AdministratorLogged; }
        }

        protected List<Link> LinksUsuario
        {
            get { return GeneralAccess.LinksUser; }
        }

        protected Parceiro ParceiroLogado
        {
            get { return PartnerAccessHelper.PartnerLogged; }
        }

        protected Redator RedatorLogado
        {
            get { return EditorAccessHelper.EditorLogged; }
        }

        protected Selecionador SelecionadorLogado
        {
            get { return RecruiterAccessHelper.RecruiterLogger; }
        }
        #endregion

        public int ParseIdQueryString(string redirectPage)
        {
            try
            {
                string id = Request.QueryString.Get("Id");

                if (string.IsNullOrEmpty(id))
                    Response.Redirect(redirectPage);

                return int.Parse(id);
            }
            catch (Exception)
            {
                Response.Redirect(redirectPage);
                throw;
            }
        }

        public int ParseIdQueryString()
        {
            return int.Parse(GetQueryString("Id"));
        }

        public object ParseQueryString(string name, Type expectedType, string redirectPage)
        {
            try
            {
                string queryString = GetQueryString(name);

                if (string.IsNullOrEmpty(queryString))
                    Response.Redirect(redirectPage);

                return ParseString(queryString, expectedType);
            }
            catch (Exception e)
            {
                Response.Redirect(redirectPage);
                throw e;
            }
        }

        private string GetQueryString(string name)
        {
            return Request.QueryString.Get(name);
        }

        public object ParseQueryString(string name, Type expectedType)
        {
            try
            {
                string queryString = GetQueryString(name);

                if (string.IsNullOrEmpty(queryString))
                    return null;

                return ParseString(queryString, expectedType);
            }
            catch (Exception)
            {
                return null;
            }
        }

        private object ParseString(string queryString, Type expectedType)
        {
            //if expectedType is string return querystring because string class don't have Parse method
            if (expectedType.Equals(typeof (String))) return queryString;

            MethodInfo methodInfo = expectedType.GetMethod("Parse", new[] {typeof (string)});

            object convertedType = methodInfo.Invoke(expectedType, new object[] {queryString});

            return convertedType;
        }

        public void TraduzirWizard(Wizard wizard)
        {
            wizard.CancelButtonText = Translator.TranslateMessage(Messages.Cancelar, AppRelativeVirtualPath);
            wizard.FinishCompleteButtonText = Translator.TranslateMessage(Messages.Finalizar, AppRelativeVirtualPath);
            wizard.StartNextButtonText = Translator.TranslateMessage(Messages.Avancar, AppRelativeVirtualPath);
            wizard.StepNextButtonText = Translator.TranslateMessage(Messages.Avancar, AppRelativeVirtualPath);
            wizard.StepPreviousButtonText = Translator.TranslateMessage(Messages.Voltar, AppRelativeVirtualPath);
        }

        public TableStructure CreateTableStructure(string caption, string summary, string cssClass)
        {
            return new TableStructure
                       {
                           Caption = caption,
                           Summary = summary,
                           CssClass = cssClass
                       };
        }

        public static string CortarTexto(string texto, int quantidadeCaracteres)
        {
            if (texto.Length <= quantidadeCaracteres) return texto;

            string textoCortado = texto.Substring(0, quantidadeCaracteres);
            string ultimaPalavra = textoCortado.Split(' ').Last();
            textoCortado = string.Concat(textoCortado.Substring(0, quantidadeCaracteres - ultimaPalavra.Length), "...");
            return textoCortado;
        }

        public string ResolverCaminhoImagem(string nomeArquivo)
        {
            if (string.IsNullOrEmpty(nomeArquivo)) return string.Empty;

            switch (nomeArquivo.Split('.').Last().ToUpper())
            {
                case "DOC":
                case "DOCX":
                    return Constants.Image.Doc;
                case "PDF":
                    return Constants.Image.Pdf;
                default:
                    return Constants.Image.UnknownFile;
            }
        }
    }
}